//
//  XDHappyMgrViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/2.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDHappyMgrViewController.h"
#import "XDImageButton.h"

#import "XDWebPageMgrViewController.h"
#import "XDVideoMgrViewController.h"
#import "XDVideoMgrModel.h"

#import "XDGameMgrViewController.h"
#import "XDShutDownViewController.h"
@interface XDHappyMgrViewController ()
/** 数据源 */
@property(nonatomic,strong) NSMutableArray *dataArr;

/** 视频时间管理模型 */
@property(nonatomic,strong) XDVideoMgrModel *videoMgrModel;

/** 游戏管理模型 */
@property(nonatomic,strong) XDVideoMgrModel *gameMgrModel;
@end


@implementation XDHappyMgrViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getVideoMgrTimeNetwork];
    
    [self getGameMgrTimeNetwork];
    
    [self initUI];
}


-(NSMutableArray *)dataArr{

    if(!_dataArr){
    
        _dataArr = [NSMutableArray arrayWithObjects:
                    @{@"title":@"影视管理",@"image":@"manage_icon_video_default"},
                    @{@"title":@"游戏管理",@"image":@"manage_icon_game_default"},
                    @{@"title":@"网页管理",@"image":@"manage_icon_web_default"},
                    @{@"title":@"关机管理",@"image":@"manage_icon_off_default"},
                    nil];
    }
    return _dataArr;
}

//创建button
-(void)initUI{
    
    CGFloat buttonW = 56;
    CGFloat buttonH = 112;
    CGFloat padding =   (ScreenW - (4 * buttonW))/5;

    for (int i = 0; i < self.dataArr.count; i ++) {
        
        XDImageButton *button = [[XDImageButton alloc]init];
        
        button.frame = CGRectMake(padding + (buttonW + padding) * i, 30, buttonW, buttonH);
        
        NSString *title = self.dataArr[i][@"title"];
        NSString *icon = self.dataArr[i][@"image"];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0x4c4c4c) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        button.tag = i;
        
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
}

//点击事件
-(void)buttonClicked:(UIButton *)btn{
    
    switch (btn.tag) {
        case 0:{     //影视管理
            
            if ([self.videoMgrModel.qid isKindOfClass:[NSNull class]]) {
                [MBProgressHUD showError:@"获取影视管理数据失败"];
                return;
            }
            XDVideoMgrViewController *vc = [[XDVideoMgrViewController alloc]init];
            self.definesPresentationContext = YES;
            vc.videoMgrModel = self.videoMgrModel;
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [self presentViewController:vc animated:YES completion:nil];
        }
            break;
        case 1:{     //游戏管理
            if ([self.gameMgrModel.qid isKindOfClass:[NSNull class]]) {
                [MBProgressHUD showError:@"获取游戏管理数据失败"];
                return;
            }
            XDGameMgrViewController *vc = [[XDGameMgrViewController alloc]init];
            self.definesPresentationContext = YES;
            vc.videoMgrModel = self.gameMgrModel;
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [self presentViewController:vc animated:YES completion:nil];
        }
            break;
        case 2:{     //网页管理
            
            XDWebPageMgrViewController *vc = [[XDWebPageMgrViewController alloc]init];
            vc.title = @"网页管理";
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
        case 3:{     //关机管理
            XDShutDownViewController *vc = [[XDShutDownViewController alloc]init];
            self.definesPresentationContext = YES;
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [self presentViewController:vc animated:YES completion:nil];
        }
            break;
            
        default:
            break;
    }
    
}


-(void)getVideoMgrTimeNetwork{
    
    NSString *url = BaseURL(@"yipark.do?action=getTime");
    
    NSDictionary *dict = @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"type":@"1"};
    
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        NSLog(@"%@",responseObj);
        XDVideoMgrModel *model = [XDVideoMgrModel modelWithDict:responseObj];
        self.videoMgrModel = model;
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


-(void)getGameMgrTimeNetwork{
    
    NSString *url = BaseURL(@"yipark.do?action=getTime");
    
    NSDictionary *dict = @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"type":@"2"};
    
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        NSLog(@"%@",responseObj);
        XDVideoMgrModel *model = [XDVideoMgrModel modelWithDict:responseObj];
        self.gameMgrModel = model;
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}
@end
